package com.design.pattern.createmode.singleton;

import java.util.HashMap;
import java.util.Map;

public class SingletonExample_04_01 {

    private SingletonExample_04_01 singleton = null;

    private static Map<String, Object> singletonMap = new HashMap();

    protected SingletonExample_04_01(){};

    public static SingletonExample_04_01 getSingleton(String singletonName){
        if(singletonMap.get(singletonName) == null){
            try {
                Object obj = Class.forName(singletonName).newInstance();
                if(obj != null && obj instanceof SingletonExample_04_01){
                    singletonMap.put(singletonName, obj);
                }
            } catch (InstantiationException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
        }
        return (SingletonExample_04_01)singletonMap.get(singletonName);
    }
}
